package com.mmdccj.yugu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mmdccj.yugu.dto.MusicListDto;
import com.mmdccj.yugu.entity.MusicLinkList;
import com.mmdccj.yugu.entity.MusicList;
import com.mmdccj.yugu.mapper.CommentMapper;
import com.mmdccj.yugu.mapper.MusicListMapper;
import com.mmdccj.yugu.service.MusicListService;
import com.mmdccj.yugu.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * @Author: MMDCCJ
 * @date: 2023-11-27 23:56
 */
@Service
public class MusicListServiceImpl implements MusicListService {
    // 自动注入MusicListMapper实例
    @Autowired
    private MusicListMapper musicListMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private MusicService musicService;

    /**
     * 创建一条歌单
     *
     * @param list 歌单中的音乐
     */
    @Override
    public void insertMusicList(MusicList list) {
        musicListMapper.insertMusicList(list);
    }

    /**
     * 查询 8 条歌单
     *
     * @return List<MusicList>
     */
    @Override
    public List<MusicList> selectMusicList() {
        return musicListMapper.selectMusicList();
    }

    /**
     * 将音乐和歌单建立连接
     *
     * @param musicListId 歌单id
     * @param musicId     音乐id
     */
    @Override
    public void insertLink(String musicListId, String musicId) {
        musicListMapper.insertLink(musicListId, musicId);
    }

    @Override
    public List<MusicList> getMusicListByUserId(String id) {
        if (id == null) {
            return null;
        }
        return musicListMapper.getMusicListByUserId(id);
    }

    /**
     * 获取歌单的图片src
     *
     * @param id 歌单id
     * @return MusicList 歌单信息
     */
    @Override
    public MusicList getListImageFrom(String id) {
        return musicListMapper.getListImageFrom(id);
    }

    /**
     * 更新歌单图片的Src
     *
     * @param src 更新后的文件名
     * @param id  歌单id
     */
    @Override
    public void updateImage(String src, String id) {
        musicListMapper.updateImage(src, id);
    }

    @Override
    public MusicList editListInfo(String id) {
        if (id != null) {
            return musicListMapper.getMusicListTitleAndIntroById(id);
        }
        return null;
    }

    /**
     * 更新歌单信息
     *
     * @param id    歌单id
     * @param intro 介绍
     * @param title 标题
     */
    @Override
    public void updateInfo(String id, String intro, String title) {
        musicListMapper.updateInfo(id, intro, title);
    }

    /**
     * 获取用户的所有歌单
     *
     * @param id 用户id
     * @return string 歌单id
     */
    @Override
    public List<MusicList> getUserMusicList(String id) {
        List<MusicList> list = musicListMapper.getUserALlList(id);
        for (MusicList m : list) {
            m.setMusicNum(musicListMapper.getMusicNumFromMusicListByMusicListId(m.getId()));
        }
        return list;
    }

    /**
     * 收藏歌单内
     *
     * @param userId      用户id
     * @param musicListId 歌单id
     * @return boolean 是否成功
     */
    @Override
    public boolean collectMusicList(String userId, String musicListId) {
        return musicListMapper.collectMusicList(userId, musicListId) > 0;
    }

    /**
     * 检查是否已经收藏该歌单
     *
     * @param userId      用户id
     * @param musicListId 歌单id
     * @return boolean 是否成功
     */
    @Override
    public boolean checkCollected(String userId, String musicListId) {
        return musicListMapper.checkCollect(userId, musicListId) > 0 || musicListMapper.isCreator(userId, musicListId) > 0;
    }

    /**
     * 检查是否存在该歌单
     *
     * @param musicListId 歌单id
     * @return boolean 存在与否
     */
    @Override
    public boolean isMusicList(String musicListId) {
        return musicListMapper.isMusicList(musicListId) == 1;
    }

    /**
     * 获取所有收藏的歌单
     *
     * @param userId 用户id
     * @return List<MusicList>
     */
    @Override
    public List<MusicList> getCollectList(String userId) {
        List<String> collectIds = musicListMapper.getCollectListIds(userId);
        if (collectIds.size() == 0) {
            return null;
        }
        List<MusicList> list = musicListMapper.getCollectList(collectIds);
        for (MusicList m : list) {
            m.setMusicNum(musicListMapper.getMusicNumFromMusicListByMusicListId(m.getId()));
        }
        return list;
    }

    @Override
    public PageInfo<MusicListDto> getMusicList(int currentPage) {
        PageHelper.startPage(currentPage, 20);
        return new PageInfo<>(musicListMapper.getMusicListDto());
    }

    @Override
    public List<MusicList> get10CD() {
        return musicListMapper.select10CD();
    }

    @Override
    public void insertCD(String userId, String title, String image, String intro) {
        musicListMapper.insertCD(userId, title, image, intro, LocalDateTime.now());
    }

    @Override
    public List<MusicList> getUserCD(String userId) {
        return musicListMapper.getUSERAllCD(userId);
    }

    @Override
    public MusicList getCDEditInfo(String userId, String id) {
        return musicListMapper.getCDEditInfo(userId, id);
    }

    @Override
    public void updateCDInfo(String userId, String id, String title, String introduce, String image) {
        musicListMapper.updateCDInfo(userId, id, title, introduce, image);
    }

    @Override
    public void insertMusicToCD(List<MusicLinkList> link) {
        musicListMapper.insertMusicToCD(link);
    }

    @Override
    public void deleteMusicFromCD(String listId, String musicId) {
        musicListMapper.removeMusicFromCD(listId, musicId);
    }

    @Override
    @Transactional
    public void deleteCD(String id, String userId) {
        musicListMapper.deleteCD(id, userId);
        musicListMapper.deleteLink(id);
        commentMapper.deleteComment(id, "cd");
        musicListMapper.deleteCollect(id);
    }

    @Override
    public PageInfo<MusicList> getRecommendList(String listId) {
        int allNums = musicListMapper.selectAll();
        int maxPage = allNums / 5;
        Random random = new Random();
        int currentPage = random.nextInt() % maxPage;
        PageHelper.startPage(currentPage, 5);
        return new PageInfo<>(musicListMapper.getRecommendList(listId));

    }

    @Override
    public void collectAllMusic(List<String> musicIds, String listId) {
        for (String musicId : musicIds) {
            if (musicService.getMusicById(musicId) != null) {
                if (musicListMapper.isInMusicList(musicId, listId) == 0) {
                    this.insertLink(listId, musicId);
                }
            }
        }
    }
}
